﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.Xpo;
using System.Runtime.Serialization;
namespace ChuanPlus.DAO
{
    class ProductDAO
    {
        //
        //  
        //  
        public static Product GetProduct(int ID)
        {
            Session session = new Session();
            XPQuery<Product> products = new XPQuery<Product>(session);
            var list = (from p in products
                        where (p.ID == ID)
                        select p).Take(1);
            List<Product> arr = new List<Product>(list.AsEnumerable<Product>());
            return arr[0];       
        }
        /// <summary>
        /// Check product if it exsit or not
        /// </summary>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        public static bool IsProductExsit(string itemCode,int distributorID)
        {
            XPQuery<Product> products = new XPQuery<Product>(Session.DefaultSession);
            var list = (from p in products
                        where (p.ItemCode == itemCode && p.DistributorID.DistributorID == distributorID)
                        select p).Take(1);
            if (list.Count() > 0)
            {
            	return true;
            }
            return false;
        }
        /// <summary>
        /// Get Alterable Product from Product
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAlterableProduct()
        {
            try
            {
                return DataProvider.execute("getAlterableProduct", null);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
    }
}
